package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.platform.dao.IProjectSupplierDao;
import com.financing.platform.dao.IProjectSupplierFlowDao;
import com.financing.platform.dto.ProjectSupplierDTO;
import com.financing.platform.dto.ProjectSupplierFlowDTO;
import com.financing.platform.service.IProjectSupplierService;

@Service("projectSupplierServiceImpl")
public class ProjectSupplierServiceImpl extends BaseServiceImpl implements
		IProjectSupplierService {

	@Autowired
	private IProjectSupplierDao projectSupplierDaoImpl;
	
	@Autowired
	private IProjectSupplierFlowDao projectSupplierFlowDaoImpl;

	@Override
	public List<ProjectSupplierDTO> getProjectSupplierList(
			ProjectSupplierDTO projectSupplierDTO) throws SQLException {
		return projectSupplierDaoImpl.getProjectSupplierList(projectSupplierDTO);
	}

	@Override
	public ProjectSupplierDTO getProjectSupplierById(ProjectSupplierDTO projectSupplierDTO)
			throws SQLException {
		return projectSupplierDaoImpl.getProjectSupplierById(projectSupplierDTO);
	}

	@Override
	public int updateProjectSupplier(ProjectSupplierDTO projectSupplierDTO)
			throws SQLException {
		
		//1、修改供应方
		int i = projectSupplierDaoImpl.updateProjectSupplier(projectSupplierDTO);
		
		if(i>0){
			ProjectSupplierFlowDTO projectSupplierFlowDTO = new ProjectSupplierFlowDTO();
			projectSupplierFlowDTO.setAccessId(projectSupplierDTO.getId());
			projectSupplierFlowDTO.setAccessCompany(projectSupplierDTO.getAccessCompany());
			projectSupplierFlowDTO.setAccessEmail(projectSupplierDTO.getAccessEmail());
			projectSupplierFlowDTO.setAccessMobile(projectSupplierDTO.getAccessMobile());
			projectSupplierFlowDTO.setAccessName(projectSupplierDTO.getAccessName());
			projectSupplierFlowDTO.setAccessPosition(projectSupplierDTO.getAccessPosition());
			projectSupplierFlowDTO.setCreateBy(projectSupplierDTO.getUpdateBy());
			projectSupplierFlowDTO.setCreateDate(new Date(System.currentTimeMillis()));
			projectSupplierFlowDTO.setUpdateBy(projectSupplierDTO.getUpdateBy());
			projectSupplierFlowDTO.setUpdateDate(new Date(System.currentTimeMillis()));
			projectSupplierFlowDTO.setSupplierAddress(projectSupplierDTO.getSupplierAddress());
			projectSupplierFlowDTO.setRemark(projectSupplierDTO.getRemark());
		//3、添加修改后的供应方到历史表
			projectSupplierFlowDaoImpl.addProjectSupplierFlow(projectSupplierFlowDTO);
		}
		return 0;
	}

	@Override
	public int addProjectSupplier(ProjectSupplierDTO projectSupplierDTO)
			throws SQLException {
		int id = projectSupplierDaoImpl.addProjectSupplier(projectSupplierDTO);
		ProjectSupplierFlowDTO projectSupplierFlowDTO = new ProjectSupplierFlowDTO();
		projectSupplierFlowDTO.setAccessId(id);
		projectSupplierFlowDTO.setAccessCompany(projectSupplierDTO.getAccessCompany());
		projectSupplierFlowDTO.setAccessEmail(projectSupplierDTO.getAccessEmail());
		projectSupplierFlowDTO.setAccessMobile(projectSupplierDTO.getAccessMobile());
		projectSupplierFlowDTO.setAccessName(projectSupplierDTO.getAccessName());
		projectSupplierFlowDTO.setAccessPosition(projectSupplierDTO.getAccessPosition());
		projectSupplierFlowDTO.setCreateBy(projectSupplierDTO.getCreateBy());
		projectSupplierFlowDTO.setCreateDate(new Date(System.currentTimeMillis()));
		projectSupplierFlowDTO.setUpdateBy(projectSupplierDTO.getUpdateBy());
		projectSupplierFlowDTO.setUpdateDate(new Date(System.currentTimeMillis()));
		projectSupplierFlowDTO.setSupplierAddress(projectSupplierDTO.getSupplierAddress());
		projectSupplierFlowDTO.setRemark(projectSupplierDTO.getRemark());
		projectSupplierFlowDaoImpl.addProjectSupplierFlow(projectSupplierFlowDTO);
		return id;
	}

	@Override
	public int queryProjectSupplierCount(ProjectSupplierDTO projectSupplierDTO)
			throws SQLException {
		return projectSupplierDaoImpl.queryProjectSupplierCount(projectSupplierDTO);
	}

	@Override
	public int delProjectSupplier(int projectSupplierId) throws SQLException {
		return projectSupplierDaoImpl.delProjectSupplier(projectSupplierId);
	}

	@Override
	public List<ProjectSupplierFlowDTO> getProjectSupplierFlowList(
			ProjectSupplierFlowDTO projectSupplierFlowDTO) throws SQLException {
		return projectSupplierFlowDaoImpl.getProjectSupplierFlowList(projectSupplierFlowDTO);
	}

	@Override
	public int queryProjectSupplierFlowCount(
			ProjectSupplierFlowDTO projectSupplierFlowDTO) throws SQLException {
		return projectSupplierFlowDaoImpl.queryProjectSupplierFlowCount(projectSupplierFlowDTO);
	}
	

}
